package com.zhuchaooasystem.base;

import android.app.Activity;
import android.app.Dialog;

import com.socks.library.KLog;
import com.zhuchaooasystem.api.HttpApi;
import com.zhuchaooasystem.api.HttpFactory;
import com.zhuchaooasystem.utils.DialogUtils;
import com.zhuchaooasystem.utils.ToastUtil;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 *
 */
public abstract class BasePresenter<T extends IView> implements IPresenter {

    protected Activity mActivity;
    protected T mView;
    protected CompositeSubscription mCompositeSubscription;
    protected static final HttpApi mHttpApi = HttpFactory.getHttpApi();
    private Dialog loadingDiaog;


    public BasePresenter(Activity activity, T view) {
        this.mActivity = activity;
        this.mView = view;
    }


    protected void showLoading(String text) {
        loadingDiaog = DialogUtils.LoadingDialog(mActivity, text);
        if (!mActivity.isFinishing()) {
            loadingDiaog.show();
        }
    }

    protected void showLoading() {
        loadingDiaog = DialogUtils.LoadingDialog(mActivity);
        if (!mActivity.isFinishing()) {
            loadingDiaog.show();
        }
    }


    protected void LoadingDiaogDismiss() {

        if (loadingDiaog != null) {
            loadingDiaog.dismiss();
        }

    }


    protected void showHint(String msg) {
        ToastUtil.showShort(mActivity, msg);
    }


    protected void handleError(Throwable throwable) {
        KLog.i("接口错误信息：" + throwable.toString());
//        ToastUtil.showShort(mActivity, ErrorHanding.handleError(throwable));
        ToastUtil.showShort(mActivity,"接口连接失败,请重试");
    }


    protected void handleError(String text) {
        ToastUtil.showShort(mActivity, text);
    }


    protected void unSubscribe() {
        if (mCompositeSubscription != null) {
            mCompositeSubscription.unsubscribe();
        }
    }

    protected void addSubscrebe(Subscription subscription) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(subscription);
    }

    @Override
    public void detachView() {
        this.mView = null;
        unSubscribe();
    }
}
